package com.infoland.afanmall.base.view.video;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.blankj.utilcode.util.SizeUtils;
import com.infoland.afanmall.R;
import com.infoland.afanmall.base.util.LogKit;

import org.raphets.roundimageview.RoundImageView;

import cn.jzvd.JZMediaManager;
import cn.jzvd.JZUserAction;
import cn.jzvd.JZUtils;
import cn.jzvd.JZVideoPlayerStandard;

/**
 * @author 王永吉
 * @date 2018/8/9 12:58
 */
public class MyJiaoZiVideo extends JZVideoPlayerStandard {

    VideoRestart videoRestart;
    public ImageView startButton;
    RoundImageView roundImageView;


    //    CURRENT_STATE_NORMAL            从没播放过的状态  第一次点击播放按钮

    //    CURRENT_STATE_AUTO_COMPLETE     播放完毕 点击了播放按钮
    @Override
    public int getLayoutId() {
        return R.layout.jiaozi_layout;
    }

    @Override
    public void init(Context context) {
        super.init(context);
        startButton = findViewById(R.id.start);
        roundImageView = findViewById(R.id.thumb);

        startButton.setOnClickListener(v -> {
            LogKit.E("覆盖 ---->   ", "  ");
            Log.i(TAG, "onClick start [" + this.hashCode() + "] ");

            if (dataSourceObjects == null || JZUtils.getCurrentFromDataSource(dataSourceObjects, currentUrlMapIndex) == null) {
                LogKit.E("dataSourceObjects ---->   ", "  dataSourceObjects");
                Toast.makeText(getContext(), getResources().getString(cn.jzvd.R.string.no_url), Toast.LENGTH_SHORT).show();
                return;
            }
            if (currentState == CURRENT_STATE_NORMAL) {
                LogKit.E("第一次点击播放 ---->   ", "  CURRENT_STATE_NORMAL");
                if (!JZUtils.getCurrentFromDataSource(dataSourceObjects, currentUrlMapIndex).toString().startsWith("file") && !
                        JZUtils.getCurrentFromDataSource(dataSourceObjects, currentUrlMapIndex).toString().startsWith("/") &&
                        !JZUtils.isWifiConnected(getContext()) && !WIFI_TIP_DIALOG_SHOWED) {
                    showWifiDialog();
                    return;
                }
                startVideo();
                onEvent(JZUserAction.ON_CLICK_START_ICON);
            } else if (currentState == CURRENT_STATE_PLAYING) {
                LogKit.E("播放中途点击 ---->   ", "  CURRENT_STATE_PLAYING");
                onEvent(JZUserAction.ON_CLICK_PAUSE);
                Log.d(TAG, "CURRENT_STATE_PLAYING  pauseVideo [" + this.hashCode() + "] ");
                JZMediaManager.pause();
                onStatePause();
            } else if (currentState == CURRENT_STATE_PAUSE) {
                LogKit.E("CURRENT_STATE_PAUSE ---->   ", "  CURRENT_STATE_PAUSE");
                onEvent(JZUserAction.ON_CLICK_RESUME);
                JZMediaManager.start();
                onStatePlaying();
            } else if (currentState == CURRENT_STATE_AUTO_COMPLETE) {
                LogKit.E("播放完毕点击 ---->   ", "  CURRENT_STATE_AUTO_COMPLETE");
                onEvent(JZUserAction.ON_CLICK_START_AUTO_COMPLETE);
                startVideo();
                if (videoRestart != null) {
                    videoRestart.restart();
                }
            }
        });
    }

    public void setVideoImgRadius(int leftTop, int rightTop, int leftBottom, int rightBottom) {
        roundImageView.setRightTopCornerRadius(rightTop);
        roundImageView.setLeftBottomCornerRadius(leftBottom);
        roundImageView.setRightBottomCornerRadius(rightBottom);
        roundImageView.setLeftTopCornerRadius(leftTop);
    }

    public MyJiaoZiVideo(Context context) {
        super(context);
    }

    public MyJiaoZiVideo(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    public VideoRestart getVideoRestart() {
        return videoRestart;
    }

    public void setVideoRestart(VideoRestart videoRestart) {
        this.videoRestart = videoRestart;
    }


}
